package com.yylc.cscore.component;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;

import java.util.Date;

/**
 * @author xiangnan
 * date 2018/5/7
 */
public class JobBean extends QuartzJobBean {
    private static final Logger logger = LogManager.getLogger();

    private static JobTrigger jobTrigger;

    public JobBean() {}
    public JobBean(JobTrigger jobTrigger) {
        synchronized (JobBean.class) {
            JobBean.jobTrigger = jobTrigger;
        }
    }

    @Override
    protected void executeInternal(JobExecutionContext context) throws JobExecutionException {

        try {
            String id = context.getTrigger().getJobKey().getName();
            Date time = context.getScheduledFireTime();

            logger.info("Quartz触发, id={}, time={}", id, time);
            jobTrigger.trigger(Integer.valueOf(id), time);
        } catch (Exception e) {
            logger.info("JobBean executeInternal error, e={}", e.getMessage());
        }
    }

}
